$(function(){
	
	
});

//减少
function amountReduce(id){
	 amountChange(id,'reduce');
}

//增加
function amountAdd(id){
	amountChange(id,'add');
}

//修改数量
function amountChange(id,opt){
	
	var $goods_number 	= $('#goods_number_' + id);
	var goods_number 	= parseInt($goods_number.val());
	
	if(isNaN(goods_number)){
		goods_number 	= 1;
		$goods_number.val(goods_number);
	}
	
	if(opt == 'reduce'){
		//减少
		goods_number--;
	}else{
		//增加
		goods_number++;
	}
	
	if(goods_number < 1){
		return false;
	}else if(goods_number >= 1000){
		alert('购买数量最多999');
		return false;
	}
	
	$goods_number.val(goods_number);
	
	$.post('/index.php/Cart/update',{'id':id,'goods_number':goods_number},function(data){
		if(!data.success){
			alert('商品更新数量失败!');
		}
		refresh_goods();
	},'json');
}

//删除购物车商品
function delCart(id){
	
	if(!confirm('您确实要把该商品移出购物车吗?')){
		return false;
	}
	
	$.post('/index.php/Cart/del',{'id':id},function(data){
		if(!data.success){
			alert('商品删除失败');
		}
		refresh_goods();
	},'json');
}


//刷新购物车
function refresh_goods(){
	var timestamp = new Date().getTime();
	$('#cart-goods').load('/index.php/Cart/goods?t=' + timestamp);
}


//清空购物车
function clearCart(){
	if(!confirm('您确实要清空购物车吗?')){
		return false;
	}
	
	$.post('/index.php/Cart/clear',{},function(data){
		refresh_goods();
	},'json');
}